package com.evola.conditionalformatting
{
	import mx.collections.ICollectionView;

	/**
	 * Klasa definise cilj kondicionog formatiranja.
	 * Cilj moze biti npr. boja teksta, broja pozadine, tip chart-a itd.
	 * Ciljevi su genericki tj. korisnik ih sam moze definisati.
	 *
	 * Ono sto je karakteristicno za ViewGoal jeste nacin na koji se
	 * definisu u view izrazu a to je:
	 *
	 * goal#color(uint=0x445566,alpha=0.5):@Amount...
	 *
	 * Na ovaj nacin svaki cilj ima svoj tip (gore navedeno 'color') i
	 * parametre cilja koji se definisu kao naziv=vrednost parovi razdvojeni zarezom.
	 *
	 * @author Sasa
	 *
	 */
	public class ViewGoal
	{

		public static const REGEX:RegExp=new RegExp("#.*:", "g");

		public var goalsData:Array;
		//uslovi za dodelu boje
		public var conditions:Array;

		public function ViewGoal()
		{
		}


		public function getViewGoalData(goalType:String):ViewGoalData
		{

			if (goalsData)
			{

				for each (var goalData:ViewGoalData in goalsData)
				{

					if (goalData.type == goalType)
					{

						return goalData;
					}
				}
			}

			return null;
		}

		/**
		 * Metoda vraca vrednost parametra ovog cilja formatiranja.
		 * Metoda je 'null-safe'.
		 *
		 * @param propName
		 * @return
		 *
		 */
		public function getPropertyValue(goalType:String, propName:String):String
		{

			var propValue:String;

			if (goalsData)
			{

				for each (var goalData:ViewGoalData in goalsData)
				{

					if (goalData.type != goalType)
						continue;

					var properties:Object=goalData.properties;

					if (properties && properties.hasOwnProperty(propName))
						propValue=properties[propName];
				}
			}

			return propValue;
		}

		public function doesAccept(value:Object, dataProvider:ICollectionView):Boolean
		{

			var isAccepted:Boolean=true;

			//prolazimo kroz svaki od uslova i testiramo 
			for each (var condition:ViewCondition in conditions)
			{

				var conditionAccepted:Boolean=condition.doesAccept(value, dataProvider);

				if (condition.operator == ViewOperator.AND)
				{

					//ako ne ispunjava uslov odmah prekidamo
					if (!conditionAccepted)
					{

						isAccepted=false;
						break;
					}
				}

				if (condition.operator == ViewOperator.OR)
				{

					//ako ispunjava uslov odmah prekidamo
					if (conditionAccepted)
					{

						isAccepted=true;
						break;
					}
				}

				if (condition.operator == null)
				{

					//prvi uslov, on nema operatora
					isAccepted=conditionAccepted;
				}
			}

			return isAccepted;
		}

		public function toExpressionString():String
		{

			var exp:String="goal#";

			if (goalsData)
			{
				
				var counter : int = 0;
				
				for each (var goalData:ViewGoalData in goalsData)
				{
					
					if(counter)
						exp += "|";

					exp+=goalData.toExpressionString();
					
					counter++;
				}
			}

			exp+=":";

			if (conditions)
			{

				for each (var condition:ViewCondition in conditions)
				{

					exp+=condition.toExpressionString();
				}
			}

			return exp;
		}

		/**
		 * Metoda proverava da li goal sadrzi cilj odredjenog tipa.
		 * @param goalType
		 * @return
		 *
		 */
		public function containsType(goalType:String):Boolean
		{

			var doesContain:Boolean=false;

			if (goalsData)
			{

				for each (var goalData:ViewGoalData in goalsData)
				{

					if (goalData.type == goalType)
					{

						doesContain=true;
						break;
					}
				}
			}

			return doesContain;
		}
	}
}
